<?
class Model_AdminSession {
	
	function sessionExtInit($session_info) {
		if (isset ( $session_info ['uid'] ) && $session_info) {
			$_session_extinfo ['user_info'] = $this->initUInfo ( $session_info );
		}
		$_session_extinfo ['site_info'] = $this->initSInfo ( $session_info );
		return $_session_extinfo;
	}
	
	/**
	 * session 用户信息初始化
	 */
	private function initUinfo($session_info) {
		$Admin_db = new DB_Admin ();
		$Admin_info = $Admin_db->loadInfo ( '*', array ('uid= ? ' => $session_info ['uid'] ) );
		
		if ($Admin_info) {
			$DB_Admin_Role = new DB_AdminRole ();
			$Admin_Role_Info = $DB_Admin_Role->loadInfoByPrimary ( $Admin_info ['roleid'] );
			$Admin_info ['rolename'] = $Admin_Role_Info ['rolename'];
			$Admin_info ['lastlogintime'] = date ( "Y-m-d H:i:s", $Admin_info ['lastlogintime'] );
		}
		
		return $Admin_info;
	}
	
	/**
	 * session 网站信息初始化
	 */
	private function initSinfo($session_info) {
		$_s_site_info = array ();
		$_tpl_channel = 'index';
		$_server_rqurl_arr = explode ( '/', $_SERVER ['REDIRECT_URL'] );
		if ($_server_rqurl_arr [1]) {
			$_tpl_channel = $_server_rqurl_arr [1];
		}
		
		$_s_site_info ['channel'] = $_tpl_channel;
		
		return $_s_site_info;
	}
	
	/**
	 * 更新验证码
	 */
	function updateSecCode($seccode, $sid = null) {
		if (! $sid) {
			$session_info = Zend_Registry::get ( 'CmsAdminSessionInfo' );
			$sid = $session_info ['sid'];
		}
		
		$DB_AdminSession = new DB_AdminSession ();
		$DB_AdminSession->updateSessionInfoBySid ( $sid, array ('seccode' => $seccode ) );
	}
	
	/**
	 * 验证更新验证码
	 */
	function checkSecCode($seccode, $sid = null) {
		
		if (! $sid) {
			$session_info = Zend_Registry::get ( 'CmsAdminSessionInfo' );
			$sid = $session_info ['sid'];
		}
		
		$DB_AdminSession = new DB_AdminSession ();
		$session_seccode = $DB_AdminSession->loadSessionInfoBySid ( $sid );
		
		if (strtolower ( $session_seccode ['seccode'] ) == strtolower ( $seccode )) {
			return true;
		}
		return false;
	}

}